package com.tmall.testgen.plugin.wizards.runinspring;

/*******************************************************************************
 * Copyright (c) 2000, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Alexander Fedorov <Alexander.Fedorov@borland.com>
 *     		- Bug 172000 [Wizards] WizardNewFileCreationPage should support overwriting existing resources
 *******************************************************************************/

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.part.DrillDownComposite;

import com.tmall.testgen.plugin.views.vo.MyFileContentProvider;

/**
 * Standard main page for a wizard that creates a file resource.
 * <p>
 * This page may be used by clients as-is; it may be also be subclassed to suit.
 * </p>
 * <p>
 * Subclasses may override
 * <ul>
 * <li><code>getInitialContents</code></li>
 * <li><code>getNewFileLabel</code></li>
 * </ul>
 * </p>
 * <p>
 * Subclasses may extend
 * <ul>
 * <li><code>handleEvent</code></li>
 * </ul>
 * </p>
 */
public class MyWizardSelectFilePage extends WizardPage {
	private static final int SIZING_CONTAINER_GROUP_HEIGHT = 250;

	private TreeViewer treeViewer;
	// sizing constants
	private static final int SIZING_SELECTION_PANE_WIDTH = 320;

	// show all projects by default
	private boolean showClosedProjects = true;

	private boolean isFileFilter;
	/**
	 * Creates a new file creation wizard page. If the initial resource
	 * selection contains exactly one container resource then it will be used as
	 * the default container resource.
	 * 
	 * @param pageName
	 *            the name of the page
	 * @param selection
	 *            the current resource selection
	 */
	public MyWizardSelectFilePage(String pageName, IStructuredSelection selection,boolean isFileFilter) {
		super(pageName);
		this.isFileFilter=isFileFilter;
		setTitle("select spring file");
		setDescription("选择Spring配置文件");
		setPageComplete(false);
	}

	/**
	 * (non-Javadoc) Method declared on IDialogPage.
	 */
	public void createControl(Composite parent) {
		initializeDialogUnits(parent);
		// top level group
		Composite topLevel = new Composite(parent, SWT.NONE);
		topLevel.setLayout(new GridLayout());
		topLevel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL
				| GridData.HORIZONTAL_ALIGN_FILL));
		topLevel.setFont(parent.getFont());
		createTreeViewer(topLevel, SIZING_CONTAINER_GROUP_HEIGHT);
		setControl(topLevel);
	}

	/**
	 * Returns a new drill down viewer for this dialog.
	 * 
	 * @param heightHint
	 *            height hint for the drill down composite
	 */
	protected void createTreeViewer(Composite com, int heightHint) {
		// Create drill down.
		DrillDownComposite drillDown = new DrillDownComposite(com, SWT.BORDER);
		GridData spec = new GridData(SWT.FILL, SWT.FILL, true, true);
		spec.widthHint = SIZING_SELECTION_PANE_WIDTH;
		spec.heightHint = heightHint;
		drillDown.setLayoutData(spec);

		// Create tree viewer inside drill down.
		treeViewer = new TreeViewer(drillDown, SWT.NONE);
		drillDown.setChildTree(treeViewer);
		MyFileContentProvider cp = new MyFileContentProvider();
		cp.showClosedProjects(showClosedProjects);
		treeViewer.setContentProvider(cp);
		treeViewer.setLabelProvider(WorkbenchLabelProvider
				.getDecoratingWorkbenchLabelProvider());
		treeViewer.setComparator(new ViewerComparator());
		treeViewer.setUseHashlookup(true);
		treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event
						.getSelection();
				containerSelectionChanged(selection.getFirstElement()); // allow
																		// null
			}
		});
		treeViewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				ISelection selection = event.getSelection();
				if (selection instanceof IStructuredSelection) {
					Object item = ((IStructuredSelection) selection)
							.getFirstElement();
					if (item == null) {
						return;
					}
					if (treeViewer.getExpandedState(item)) {
						treeViewer.collapseToLevel(item, 1);
					} else {
						treeViewer.expandToLevel(item, 1);
					}
				}
			}
		});

		treeViewer.setInput(ResourcesPlugin.getWorkspace());
	}

	private String selectedFilePath = "";
	private String selectedProjectPath = "";
	private IProject selectedProject = null;

	public void containerSelectionChanged(Object obj) {
		
		if (obj == null) {
			selectedFilePath = "";
			setPageComplete(false);
		} else if(isFileFilter&&(obj instanceof IFile)){
			IFile selectedFile = (IFile) obj;
			selectedFilePath = selectedFile.getLocation().toString();//$NON-NLS-1$
			selectedProjectPath = selectedFile.getProject().getLocation()
					.toString();
			selectedProject = selectedFile.getProject();
			setPageComplete(true);
		} else if (!isFileFilter &&(obj instanceof IFolder)) {
			IFolder selectedFolder = (IFolder) obj;
			selectedProjectPath = selectedFolder.getLocation().toString();
			selectedProject =selectedFolder.getProject();
			setPageComplete(true);
		} else {
			selectedFilePath = "";//$NON-NLS-1$
			setPageComplete(false);
		}

	}

	public IProject getSelectedProject() {
		return selectedProject;
	}

	public String getFilePath() {
		return selectedFilePath;
	}

	public String getObjectPath() {
		return selectedProjectPath;
	}

}
